<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Pricechange_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 添加价格变动记录
	 */
	public function create($pricechange) {
		$this->db->insert ( 't_pricechange', $pricechange );
		return $this->db->insert_id ();
	}
	/*
	 * 根据分页信息返回价格变动记录
	 */
	public function find_all_pricechanges($name,$per_page,$offset){
		$this->db->select('t_pricechange.id,t_product.name,t_product.unit,t_pricechange.oldprice,t_pricechange.price,t_pricechange.update_time');
		$this->db->from('t_pricechange');
		$this->db->join('t_product', 't_product.id = t_pricechange.productid','left');
		if(!empty($name)){
			$this->db->like('name', $name);
		}
		$this->db->limit($per_page, $offset);
		$this->db->order_by("id", "desc");
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 返回价格变动记录总数
	 */
	public function count_pricechanges($name){
		$this->db->join('t_product', 't_product.id = t_pricechange.productid','left');
		if(!empty($name)){
			$this->db->like('name', $name);
		}
		$query = $this->db->get('t_pricechange');
		return $query->num_rows();
	}
	/*
	 * 根据日期得到价格变动信息
	 */
	public function find_pricechanges_by_date($date){//
		$this->db->select('t_pricechange.id,t_product.name,t_product.unit,t_pricechange.oldprice,t_pricechange.price,t_pricechange.update_time');
		$this->db->from('t_pricechange');
		$this->db->join('t_product', 't_product.id = t_pricechange.productid','left');
		$this->db->where("DATE_FORMAT(update_time,'%Y-%m-%d')=",$date);
		$query = $this->db->get();
		return $query->result_array();
	}
}